package com.crawljax.browser;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

public class ImageUtils {

    /**
     *
     *
     * @param input
     * @param x
     * @param y
     * @param width
     * @param height
     * @return
     */
    public static BufferedImage cut(File input, int x, int y, int width, int height){

        try {
            BufferedImage bi = ImageIO.read(input);
            int srcWidth = bi.getWidth();
            int srcHeight = bi.getHeight();
            System.out.println("src:" + srcWidth + " "+srcHeight);
            if(srcHeight < height || srcWidth < width){
                return bi;
            }
            BufferedImage ans = bi.getSubimage(x, y, width, height);

            return ans;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 测试成功
    public void testCut() throws IOException{
        String src = "E:/tmp/1.jpg";
        String des = "E:/tmp/1_cut.jpg";
        BufferedImage srcB =ImageIO.read(new File(src));
        BufferedImage ans = cut(new File(src), 0, srcB.getHeight()/2, srcB.getWidth(), srcB.getHeight()/2);
        File put = new File(des);
        if(put.exists())put.delete();
        try {
            ImageIO.write(ans, "png", put);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    /**
     * 为了方便起见，我们传入要合并后的图片的大小，否则也可以???过??有的pics来获??
     *
     * @param pics  ??有要合并图片的列??
     * @param width  新图片的大小
     * @param height
     * @return
     */
    public static BufferedImage mergerImages(List<BufferedImage> pics, int width, int height){

        int picNum = pics.size();
        BufferedImage newImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

        BufferedImage tempImage;
        int w,h;
        int []imageArray;
        //int [][]allImageArray = new int[picNum][];
        int heightPart=0;
        for(int i=0;i<picNum;i++){
            tempImage = pics.get(i);
            w = tempImage.getWidth();
            h = tempImage.getHeight();
            imageArray = new int[w*h];
            imageArray = tempImage.getRGB(0, 0, w, h, imageArray, 0, w);
            newImage.setRGB(0, heightPart, w, h, imageArray, 0,w);
            heightPart+=h;
            //allImageArray[i] = imageArray;
        }
        // 对最后合并的图片剪切去掉旁边的滚??

        return newImage.getSubimage(0, 0, newImage.getWidth()-15, newImage.getHeight());

    }

    public void testMerger(){
        String fileName = "E://tmp//";
        String tempName;
        BufferedImage tempBuff;
        int w=0,h=0;
        List<BufferedImage> allImage= new LinkedList<BufferedImage>();
        for(int i =1; i < 7; i++){
            tempName = fileName + i+".jpg";
            try {
                tempBuff = ImageIO.read(new File(tempName));
                w = tempBuff.getWidth();
                h +=tempBuff.getHeight();
                allImage.add(tempBuff);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        BufferedImage ans = mergerImages(allImage, w, h);
        try {
            ImageIO.write(ans,"png",new File(fileName+"merge.png"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}